<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * This element uses jQuery widget DatePicker from jquery ui library.
 *
 * Some of widget's options are configurable through it
 * To use it don't forget to add jquery ui library
 *
 * @author chaporgin_a
 */
class VA_Form_FormElement_Date extends VA_Form_FormElement {
    private $closeText = null;
    /**
     * Text string to place after datepicker field
     * @var string
     */
    private $appendText = '';
    /**
     * Allows to change month
     * @var boolean
     */
    private $changeMonth = TRUE;
    /**
     * Allows to change year
     * @var boolean
     */
    private $changeYear = TRUE;
    /**
     * Текст который используется для ссылки на сегодняшний день
     * @var string
     */
    private $currentText = null;
    private $dateFormat = 'yy/mm/dd';
    private $dayNames = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    private $dayNamesMin = array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
    /**
     * Set the first day of the week: Sunday is 0, Monday is 1,
     * @var int
     */
    private $firstDay = 1;
    /**
     * Set a maximum selectable date via a Date object,
     * or a number of days from today (e.g. +7) or a string of values
     * and periods ('y' for years, 'm' for months, 'w' for weeks,
     * 'd' for days, e.g. '+1m +1w'), or null for no limit.
     * @var mixed
     */
    private $maxDate = null;
    /**
     * Set a minimum selectable date via a Date object,
     * or a number of days from today (e.g. +7)
     * or a string of values and periods ('y' for years,
     * 'm' for months, 'w' for weeks, 'd' for days, e.g. '-1y -1m'),
     * or null for no limit.
     * @var mixed
     */
    private $minDate = null;
    private $monthNames = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    private $monthNamesShort = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    private $nextText = 'Next';
    /**
     * Set how many months to show at once.
     * The value IS a straight integer
     * @var int
     */
    private $numberOfMonths = 1;
    private $prevText = 'Prev';
    /**
     * Whether to show the button panel.
     * @var boolean
     */
    private $showButtonPanel = TRUE;
    /**
     * Have the datepicker appear automatically when the
     * field receives focus ('focus'),
     * appear only when a button is clicked ('button'),
     * or appear when either event takes place ('both').
     * @var string
     */
    private $showOn = 'both';
    /**
     * Control the speed at which the datepicker appears,
     * it may be a time in milliseconds,
     * a string representing one of the three predefined speeds
     * ("slow", "normal", "fast"), or '' for immediately.
     */
    private $duration = '';
    public function  __construct() {
        parent::__construct();
        $this->minDate = strtotime('1960/01/01');
        $this->maxDate = strtotime('+31 day');
        $this->closeText = 'Закрыть';
        $this->currentText = 'сегодня';
        $this->dayNames = array('Воскресенье', 'Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота');
        $this->dayNamesMin = array('Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб');
        $this->monthNames = array('Январь', 'Февраль', 'Март', 'Апрель', 'Март', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь');
        $this->monthNamesShort = array('Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек');
        $this->nextText = 'Вперед';
        $this->prevText = 'Обратно';
    }
    public function getPrintableValue() {
        return $this->getValue();
    }
    public function getValue() {
        if(empty($this->value)) $this->value = date('Y/m/d');
        return $this->value;
    }
    public function getDateFormat() {
        return $this->dateFormat;
    }
    public function getCloseText() {
        return $this->closeText;
    }
    public function getAppendText() {
        return $this->appendText;
    }
    public function getChangeMonth() {
        return $this->changeMonth;
    }
    public function getChangeYear() {
        return $this->changeYear;
    }
    public function getCurrentText() {
        return $this->currentText;
    }
    public function getDayNames() {
        return $this->dayNames;
    }
    public function getDayNamesMin() {
        return $this->dayNamesMin;
    }
    public function getMinDate() {
        return $this->minDate;
    }
    public function getMaxDate() {
        return $this->maxDate;
    }
    public function getMonthNames() {
        return $this->monthNames;
    }
    public function getMonthNamesShort() {
        return $this->monthNamesShort;
    }
    public function getNextText() {
        return $this->nextText;
    }
    public function getPrevText() {
        return $this->prevText;
    }
    public function getNumberOfMonths() {
        return $this->numberOfMonths;
    }
    public function getShowButtonPanel() {
        return $this->showButtonPanel;
    }
    public function getShowOn() {
        return $this->showOn;
    }
    public function getDuration() {
        return $this->duration;
    }
    public function setMinDate($v) {
        $this->minDate = $v;
        return $this;
    }
    public function setMaxDate($v) {
        $this->maxDate = $v;
        return $this;
    }
    public function setNumberOfMonths($v) {
        $this->numberOfMonths = $v;
        return $this;
    }
    public function setShowButtonPanel($v) {
        $this->showButtonPanel = $v;
        return $this;
    }
    public function getFirstDay() {
        return $this->firstDay;
    }
    public function getValidators() {
        $date = FALSE;
        foreach(parent::getValidators() as $validator) {
            if($validator instanceof VA_Form_Validator_Date) $date = TRUE;
        }
        if(FALSE == $date) {
            $v = new VA_Form_Validator_Date();
            $v->init(array());
            $this->addValidator($v);
        }
        return parent::getValidators();
    }
    protected function setDefaultDecorators() {
        $this->decorators = array();
        $this->addDecorator('ViewHelper')
                ->addDecorator('Error')
                ->addDecorator('HtmlTag', array('tag'=>'div', 'classes'=>array('element', 'date')))
                ->addDecorator('Label', array('position' => VA_Form_Decorator_Label::PREPEND))
                ->addDecorator('HtmlTag', array('tag'=>'div', 'jQueryMark'=>'controlWrapper', 'classes'=>array('elementWrapper')));
    }
}